<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Home\Controller;
use Think\Controller;
/**
 * 
 * @author jry 
 */
class OrdersController extends HomeController{
    /**
     * 默认方法
     * @author 
     */
    public function index(){
        if(empty($_GET['currency'])){
            $this->display('Public:b_stop');
            return;
        }
        $currency_id=I('get.currency');
        $currency=M('Currency')->where("currency_mark='$currency_id' and is_line=1")->find();
        
        if(empty($currency)){
            $this->display('Public:b_stop');
            return;
        }
        $currency['currency_digit_num']=$currency['currency_digit_num']?$currency['currency_digit_num']:4;//设置限制位数
        
        
        //显示委托记录
        $buy_record=getOrdersByType($currency['id'],'buy', 10, 'desc');
        $sell_record=getOrdersByType($currency['id'],'sell', 10, 'asc');
//         dump($sell_record);
//         dump($sell_record[count($sell_record)-1]['price']);//最佳买价
//         dump($buy_record);
//         dump($buy_record[0]['price']);//最佳卖价
        $this->assign('buy_record',$buy_record);
        $this->assign('sell_record',$sell_record);
        
        //个人账户资产
        $USER=session('user_auth');
          if (!empty($USER)){
            $userinfo=get_user_info($USER['uid']);
            $user_currency_money['currency']['num']=getUserMoney($currency['id'], 'num',$userinfo);
            $user_currency_money['currency']['forzen_num']=getUserMoney($currency['id'], 'forzen_num',$userinfo);
            $user_currency_money['currency_trade']['num']=getUserMoney($currency['trade_currency_id'], 'num',$userinfo);
            $user_currency_money['currency_trade']['forzen_num']=getUserMoney($currency['trade_currency_id'], 'forzen_num',$userinfo);
            if($currency['trade_currency_id']==0){
                $user_currency_money['currency_trade']['num']=$userinfo['money'];
                $user_currency_money['currency_trade']['forzen_num']=$userinfo['forzen_money'];
            }
//             dump($user_currency_money);
//             exit;
            $this->assign('user_currency_money',$user_currency_money);
            //个人挂单记录
            //$this->assign('user_orders',getOrdersByUser(5,$currency['currency_id']));
            //最大可买
            /* if (!empty($sell_record)){
                $buy_num=sprintf('%.4f',$user_currency_money['currency_trade']['num']/$sell_record[0]['price']);
            }else {
                $buy_num=0;
            }
            $this->assign('buy_num',$buy_num); */
            //最大可卖
            $sell_num=sprintf('%.4f',$user_currency_money['currency']['num']);
            $this->assign('sell_num',$sell_num);
        } 
        //最新成交量
        $new_cur=getTradesByNum('15',$currency['id']);
        foreach ($new_cur as $nk=>$nv)
        {
            $new_cur[$nk]['ctime']=time_format($nv['ctime'],'m-d H:i:s');
            $new_cur[$nk]['type']=$nv['type']=="sell"?'卖出':'买入';
        }
        $this->assign('new_curorder',$new_cur);
        //显示委托记录
        $entrust_info=getEntrustById($currency['id'],"30");
       // dump($entrust_info);
        $this->assign('entrust_info',$entrust_info);
        //格式化手续费
        $currency['currency_sell_fee']=floatval($currency['currency_sell_fee']);
        $currency['currency_buy_fee']=floatval($currency['currency_buy_fee']);
        
        //币种信息
        $usermoney="";
        $this->assign('userinfo',$USER);
        $currency_message=getCurrencyMessageById($currency['id']);
        $currency_trade=getCurrencynameById($currency['trade_currency_id']);
        $this->assign('currency_message',$currency_message);
        $this->assign('currency_trade',$currency_trade);
        //dump($currency);
        
        $USER['money']=get_user_data($USER['uid'],'money');
        $this->assign('uinfo',$USER);
        $this->assign('currency',$currency);
        $this->assign('curid',$currency['id']);
        //域名
        $wsturl=WSTDomain();
        $this->assign('wsturl',$wsturl);
        //成交记录
        $trade=getOrdersByStatus(2, 20, $currency['currency_id']);
        $this->assign('trade',$trade);
        
        $this->assign('meta_title', "币交所交易平台");
        $this->display('buyAndout');
    }
    
    public function chart()
    {
        $this->display("chart");
    }
    
    public function indexk()
    {
        $this->display("index");
    }
    
 //获取k线
    public function getOrdersKline(){
        if(empty($_GET['currency'])){
            return;
        }
        $currency_id=I('get.currency');
        //K线
        $char=!empty($_GET['time'])?I('get.time'):'001';
        switch ($char){
            case '001':$time=1;break;
            case '005':$time=5;break;
            case '015':$time=15;break;
            case '030':$time=30;break;
            case '060':$time=60;break;
            case '100':$time=24*60;break;
            case '200':$time=7*24*60;break;
            case '300':$time=30*24*60;break;
            default:$time=60;
        }
        $data[$char]=$this->getKline($time,$currency_id);
        $json_data=json_encode($data[$char]);
        echo $json_data;
//         dump($json_data);
//         exit;
//         $this->ajaxReturn($data);
    }
    
    //获取实时数据
    /**
     *
     * @param unknown $currency_id,币种id
     * @param unknown $base_time,分时,001,005,015,030,060
     */
    private function getKline($base_time,$currency_id){
            $time=time()-$base_time*60*60;
            for ($i=0;$i<60;$i++){
             $start= $time+$base_time*60*$i;
             $end=$start+$base_time*60;
            //时间
             $dat=date('YmdHis',$start);
            $item[$i][]=$dat.'00000';//$start*1000+8*3600*1000;
            $where['curid']=$currency_id;
            $where['type']='buy';
            $where['ctime']=array('between',array($start,$end));
     
            //开盘
            $where_price['curid']=$currency_id;
            $where_price['type']='buy';
            $where_price['ctime']=array('elt',$end);
          
            $order_k=M('Orders')->field('price')->where($where_price)->order('ctime desc')->find();
            $item[$i][]=!empty($order_k['price'])?floatval($order_k['price']):0;
            //最高
           $max=M('Orders')->where($where)->max('price');
           $max=!empty($max)?floatval($max):$order_k['price'];
           $max=!empty($max)?$max:0;
           $item[$i][]=$max;
            //最低
            $min=M('Orders')->where($where)->min('price');
            $min=!empty($min)?floatval($min):$order_k['price'];
            $item[$i][]=!empty($min)?$min:0;
            //收盘
            $order_s=M('Orders')->field('price')->where($where)->order('ctime asc')->find();
            $order_s=!empty($order_s['price'])?floatval($order_s['price']):$order_k['price'];
            $item[$i][]=!empty($order_s)?$order_s:0;
            //交易量
            $num=M('Orders')->where($where)->sum('num');
            //echo M()->getLastSql();
            $item[$i][]=!empty($num)?floatval($num):0;
        }
       // $item=json_encode($item,true);
        return $item;
    }
}
